package com.xxx.video.main.fragments;

import android.os.Bundle;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.xxx.video.R;
import com.xxx.video.commen.adapters.BaseRecycleViewAdapter;
import com.xxx.video.commen.configs.UserManager;
import com.xxx.video.commen.events.CommentSuccessEvent;
import com.xxx.video.commen.fragments.BaseFragment;
import com.xxx.video.commen.utils.ListUtils;
import com.xxx.video.commen.utils.StringUtils;
import com.xxx.video.commen.utils.TimeUtils;
import com.xxx.video.commen.utils.ToastUtils;
import com.xxx.video.commen.views.recyclerviews.RefreshRecyclerView;
import com.xxx.video.main.adapters.VideoDetailsCommentAdapter;
import com.xxx.video.main.beans.VideoDetailsCommentListBean;
import com.xxx.video.main.presenters.IVideoDetailsCommentView;
import com.xxx.video.main.presenters.VideoDetailsCommentPresenter;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Objects;

import butterknife.BindView;

public class VideoDetailsCommentFragment extends BaseFragment<VideoDetailsCommentPresenter> implements
        IVideoDetailsCommentView, RefreshRecyclerView.OnRecyclerViewRefreshOrLoadMoreListener,
        BaseRecycleViewAdapter.OnItemClickListener<VideoDetailsCommentListBean.PageBean.ListBean> {

    @BindView(R.id.rcv_video_details_comment_fragment_list)
    RefreshRecyclerView rcvVideoDetailsCommentFragmentList;
    /**
     * page 1
     */
    private int page = 1;

    /**
     * video Id
     */
    private String mVideoId;
    /**
     * adapter
     */
    private VideoDetailsCommentAdapter mVideoDetailsCommentAdapter;

    public static VideoDetailsCommentFragment newInstance(Bundle args) {
        VideoDetailsCommentFragment fragment = new VideoDetailsCommentFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public boolean isOpenEventBus() {
        return true;
    }

    @Override
    protected VideoDetailsCommentPresenter createPresenter() {
        return new VideoDetailsCommentPresenter(Objects.requireNonNull(getActivity()), this);
    }

    @Override
    public int getContentLayout() {
        return R.layout.fragment_video_details_comment;
    }

    @Override
    public void initData(Bundle extras) {
        showLayoutStatus(1);
        if (extras != null) {
            mVideoId = extras.getString("Id");
        }
    }

    @Override
    public void refreshData(Bundle extras) {

    }

    @Override
    public void onResume() {
        super.onResume();
        if (isFirst) {
            isFirst = false;
            getPresenter().getCommentInfo(page, mVideoId);
        }
    }

    @Override
    public void showRootLayoutStatus(int status) {
        showLayoutStatus(status);
    }


    @Override
    public void initData2View(VideoDetailsCommentListBean successOb) {
        rcvVideoDetailsCommentFragmentList.resetState();
        showRootLayoutStatus(1);
        if (mVideoDetailsCommentAdapter == null) {
            rcvVideoDetailsCommentFragmentList.setLayoutManager(new LinearLayoutManager(getActivity()));
            mVideoDetailsCommentAdapter = new VideoDetailsCommentAdapter(Objects.requireNonNull(getActivity()));
            mVideoDetailsCommentAdapter.setOnItemClickListener(this);
            rcvVideoDetailsCommentFragmentList.setAdapter(mVideoDetailsCommentAdapter);
            rcvVideoDetailsCommentFragmentList.setOnRecyclerViewRefreshOrLoadMoreListener(this);
        }
        if (page == 1) {
            if (ListUtils.isEmpty(successOb.page.list)) {
                showRootLayoutStatus(2);
            } else {
                if (successOb.page.list.size() < 10) {
                    rcvVideoDetailsCommentFragmentList.setNoMoreData();
                }
                mVideoDetailsCommentAdapter.setData(successOb.page.list);
            }
        } else {
            if (ListUtils.isEmpty(successOb.page.list) || successOb.page.list.size() < 10) {
                rcvVideoDetailsCommentFragmentList.setNoMoreData();
            }
            mVideoDetailsCommentAdapter.addData(successOb.page.list);
        }
    }

    @Override
    public void deleteCommentSuccess(int position) {
        if (mVideoDetailsCommentAdapter!=null){
            mVideoDetailsCommentAdapter.deleteCommentSuccess(position);
            ToastUtils.showSuccess(StringUtils.getInstance().getString(R.string.tip_delete_comment_success));
        }
    }


    @Override
    public void onRecyclerviewRefresh() {
        page = 1;
        getPresenter().getCommentInfo(page, mVideoId);
    }

    @Override
    public void onRecyclerviewLoadMore() {
        page++;
        getPresenter().getCommentInfo(page, mVideoId);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCommentSuccess(CommentSuccessEvent commentSuccessEvent) {
        if (mVideoDetailsCommentAdapter != null) {
            if (ListUtils.isEmpty(mVideoDetailsCommentAdapter.data)) {
                showRootLayoutStatus(1);
            }
            VideoDetailsCommentListBean.PageBean.ListBean listBean = new VideoDetailsCommentListBean.PageBean.ListBean();
            listBean.fUsePicurl = UserManager.getInstance().getUserAvatar();
            listBean.fUserName = UserManager.getInstance().getUserName();
            listBean.fCreateTime = TimeUtils.getInstance().getStringDate(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss");
            listBean.fComment = commentSuccessEvent.comment;
            mVideoDetailsCommentAdapter.addData(listBean, 0);

        }
    }

    @Override
    public void onItemClick(VideoDetailsCommentListBean.PageBean.ListBean listBean, int position, int type) {
        if (type == 1) {
            getPresenter().deleteComment(listBean.fId, position);
        }
    }
}
